//
//  RedbagBaseModel.m
//  LotteryApp
//
//  Created by Zhl on 16/11/1.
//  Copyright © 2016年 windo. All rights reserved.
//

#import "RedbagBaseModel.h"
#import <objc/runtime.h>
@implementation RedbagBaseModel
-(instancetype)initWithDictionary:(NSDictionary *)dic{
    self = [super init];
    if (self) {
        self.errorCode = [[dic objectForKey:@"errorCode"] stringValue];
        self.errorString = [dic objectForKey:@"errorString"];
//        [self decodeWithDic:dic];
    }
    return self;
}
-(void)decode:(NSString *)str{
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error];
    NSLog(@"dict == %@",dic);
    [self decodeWithDic:dic];
}
-(void)decodeWithDic:(NSDictionary *)dic{
    unsigned int propsCount;
    NSObject *obj = [[[self class] alloc] init];
    while (obj) {
        objc_property_t *props = class_copyPropertyList([obj class], &propsCount);
        for (int i = 0; i < propsCount; i++) {
            objc_property_t prop = props[i];
            id value = nil;
            @try {
                NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
                value = [dic objectForKey:propName];
                if (value != nil) {
                    if ([value isKindOfClass:[NSArray class]]) {
                        NSArray *arrValue = (NSArray*)value;
                        [self decodeWithSubArray:arrValue withPropertyName:propName];
                    }else{
                        [self setValue:value forKey:propName];
                    }
                }
            }
            @catch (NSException *exception) {
                
            }
            obj = [[obj.superclass alloc] init];
        }
    }
}
-(void)decodeWithSubArray:(NSArray *)arr withPropertyName:(NSString *)name{
}
@end
